<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xwk="http://http://code.google.com/p/xworks"
    xmlns:xwkForms="http://http://code.google.com/p/xworks.forms"
	exclude-result-prefixes="xwk xwkForms">

	<xsl:template match="forms/form">
		<xsl:param name="name" select="@name"/>
		<xsl:param name="id" select="concat('id_',$name)"/>
		<xsl:param name="action" select="$xwk:DOC_ACTION"/>
		<xsl:param name="class"/>
		<xsl:param name="method">POST</xsl:param>
		<xsl:param name="onsubmit"/>
		<xsl:param name="xwk_action"/>
		<xsl:param name="request" select="/.."/>
		<xsl:param name="visible" select="true()"/>
		
		
		<xsl:call-template name="xwkForms">
			<xsl:with-param name="name" select="$name"/>
			<xsl:with-param name="id" select="$id"/>
			<xsl:with-param name="action" select="$action"/>
			<xsl:with-param name="class" select="$class"/>
			<xsl:with-param name="method" select="$method"/>
			<xsl:with-param name="onsubmit" select="$onsubmit"/>
			<xsl:with-param name="xwk_action" select="$wap_action"/>
			<xsl:with-param name="request" select="$request"/>
			<xsl:with-param name="visible" select="$visible"/>
		</xsl:call-template>
	</xsl:template>

	<xsl:template match="forms/form" name="xwkForms" mode="xwk:Forms">
		<xsl:param name="name" select="local-name()"/>
		<xsl:param name="id" select="concat('Form_',$name)"/>
		<xsl:param name="action" select="$xwk:DOC_ACTION"/>
		<xsl:param name="class"/>
		<xsl:param name="method">POST</xsl:param>
		<xsl:param name="onsubmit"/>
		<xsl:param name="wap_action"/>
		<xsl:param name="request" select="/.."/>
		<xsl:param name="visible" select="true()"/>

		<form name="{$name}" id="{$id}" action="{$action}" method="{$method}">
			<xsl:if test="string($class)">
				<xsl:attribute name="class">
					<xsl:value-of select="$class"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="$visible=false()">
				<xsl:attribute name="style">display:none</xsl:attribute>
			</xsl:if>
			<xsl:if test="string($onsubmit)">
				<xsl:attribute name="onsubmit">
					<xsl:value-of select="$onsubmit" />
				</xsl:attribute>
			</xsl:if>

			<xsl:apply-templates select="item" mode="xwk:Forms">
				<xsl:with-param name="parameters" select="$request[command[@type='Update' or @type='Insert' or @type='Delete']]/parameters"/>
				<xsl:with-param name="row" select="$request[1]/result//data/row"/>
				<xsl:with-param name="form_id" select="$id"/>
			</xsl:apply-templates>

			<xsl:if test="string($wap_action)">
				<input type="hidden" name="wap_action"  value="{$wap_action}"/>
			</xsl:if>
		</form>	
		
	</xsl:template>

	<xsl:template match="item" mode="xwk:Forms">
		<xsl:param name="form_id"/>
		<xsl:param name="parameters" select="/.."/>
		<xsl:param name="row" select="/.."/>
		<xsl:variable name="name" select="@name"/>
		<xsl:variable name="parameter" select="$parameters/parameter[@name=$name]"/>
		<xsl:variable name="type">
			<xsl:choose>
				<xsl:when test="@type">
					<xsl:value-of select="@type"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>text</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="class">
			<xsl:choose>
				<xsl:when test="@class">
					<xsl:value-of select="@class"/>
					<xsl:if test="$parameter/@valid='false'">
						<xsl:value-of select="concat(@class, ' invalid')"/>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:if test="$parameter/@valid='false'">
						<xsl:value-of select="concat(@class, ' invalid')"/>
					</xsl:if>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<div>
			<input name="{@name}">
				
				<xsl:attribute name="type">
					<xsl:value-of select="$type"/>
				</xsl:attribute>
				<xsl:if test="@disabled='true' or @disabled='True' or @disabled='TRUE'">
					<xsl:attribute name="disabled">disabled</xsl:attribute>
				</xsl:if>

				<!-- this helps in client side styling -->
				<xsl:if test="string($class)">
					<xsl:attribute name="class">
						<xsl:value-of select="$type"/>
					</xsl:attribute>
				</xsl:if>
				
				<xsl:attribute name="value">
					<xsl:choose>
						<xsl:when test="$row/@*">
							<xsl:value-of select="$row/@*[name()=$name]"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$xwk:BROWSER_VARS//node[@name=current()/@name]"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				
				<!-- test to see if valid input for parameter -->
				<xsl:if test="$parameter/@valid='false'">
					<xsl:attribute name="class">invalid</xsl:attribute>
				</xsl:if>
			</input>
			<xsl:if test="not(@type='hidden')">
				<span>
					<xsl:value-of select="@title"/>
				</span>
			</xsl:if>
			<xsl:if test="$parameter/@valid = 'false'">
				<span class="invalid">Invalid!</span>
			</xsl:if>
			</div>
	</xsl:template>
	
	<!-- textarea -->
	<xsl:template match="item[@type='textarea']" mode="xwk:Forms">
		<xsl:param name="form_id"/>
		<xsl:param name="title"/>
		<xsl:param name="parameters" select="/.."/>
		<xsl:param name="row" select="/.."/>

		<xsl:variable name="name" select="@name"/>
		<xsl:variable name="parameter" select="$parameters/parameter[@name=$name]"/>

		<textarea>
			<xsl:if test="@name">
				<xsl:attribute name="name">
					<xsl:value-of select="@name"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@rows">
				<xsl:attribute name="rows">
					<xsl:value-of select="@rows"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@cols">
				<xsl:attribute name="cols">
					<xsl:value-of select="@cols"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@disabled='true' or @disabled='True' or @disabled='TRUE'">
				<xsl:attribute name="disabled">disabled</xsl:attribute>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="$row/@*">
					<xsl:value-of select="$row/@*[name()=$name]"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$xwk:BROWSER_VARS//node[@name=current()/@name]"/>
				</xsl:otherwise>
			</xsl:choose>
		</textarea>

		<xsl:if test="not(@type='hidden')">
			<span>
				<xsl:value-of select="@title"/>
			</span>
		</xsl:if>
		<xsl:if test="$parameter/@valid='false'">
			<span class="invalid">Invalid!</span>
		</xsl:if>

	</xsl:template>

	<!-- submit / cancel button -->
	<xsl:template match="item[@type='submit' or @type='cancel']" mode="xwk:Forms">
		<xsl:param name="form_id"/>
		<xsl:param name="title"/>

		<xsl:variable name="value">
			<xsl:choose>
				<xsl:when test="string($title)">
					<xsl:value-of select="$title"/>
				</xsl:when>
				<xsl:when test="@type='submit'">
					<xsl:text>Submit</xsl:text>
				</xsl:when>
				<xsl:when test="@type='cancel'">
					<xsl:text>Cancel</xsl:text>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		
		<input type="submit">
			<xsl:if test="@name">
				<xsl:attribute name="name">
					<xsl:value-of select="@name"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@type='cancel'">
				<xsl:attribute name="onclick">
					<xsl:text>Form.reset('</xsl:text>
					<xsl:value-of select="$form_id"/>
					<xsl:text>'); Element.hide('</xsl:text>
					<xsl:value-of select="$form_id"/>
					<xsl:text>'); return false;</xsl:text>
				</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="value">
				<xsl:value-of select="$value"/>
			</xsl:attribute>
			<xsl:if test="@disabled='true' or @disabled='True' or @disabled='TRUE'">
				<xsl:attribute name="disabled">disabled</xsl:attribute>
			</xsl:if>
			<xsl:if test="@onclick">
				<xsl:attribute name="onclick">
					<xsl:value-of select="@onclick" />
				</xsl:attribute>
			</xsl:if>
		</input>
	</xsl:template>
	
	<!-- item selector "drodpown" -->
	<xsl:template match="item[value]" mode="xwk:Forms">
		<xsl:param name="form_id"/>
		<xsl:param name="parameters" select="/.."/>
		<xsl:param name="row" select="/.."/>
		<xsl:variable name="name" select="@name"/>
		<xsl:variable name="parameter" select="$parameters/parameter[@name=$name]"/>

		<div>
			<select name="{@name}">
				<!-- test to see if valid input for parameter -->
				<xsl:if test="$parameter/@valid='false'">
					<xsl:attribute name="class">invalid</xsl:attribute>
				</xsl:if>
				<xsl:if test="@disabled='true' or @disabled='True' or @disabled='TRUE'">
					<xsl:attribute name="disabled">disabled</xsl:attribute>
				</xsl:if>

				<xsl:apply-templates select="value" mode="xwk:Forms">
					<xsl:with-param name="selected_value" select="$row/@*[name()=$name]"/>
				</xsl:apply-templates>
			</select>
			<span>
				<xsl:value-of select="@title"/>
			</span>
			<xsl:if test="$parameter/@valid='false'">
				<span class="invalid">Invalid!</span>
			</xsl:if>

		</div>
	</xsl:template>

	<xsl:template match="value" mode="xwk:Forms">
		<xsl:param name="selected_value"/>
		<xsl:variable name="element_name" select="../@name"/>
		
		<option>

			<!-- option value -->
			<xsl:attribute name="value">
				<xsl:value-of select="."/>
			</xsl:attribute>

			<!-- option selected -->
			<xsl:if test="$selected_value = . or $xwk:BROWSER_VARS//node[@name=$element_name] = .">
				<xsl:attribute name="selected"/>
			</xsl:if>

			<!-- option title / display -->
			<xsl:choose>
				<xsl:when test="@title">
					<xsl:value-of select="@title"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
			
		</option>

	</xsl:template>

</xsl:stylesheet> 

